]> dgit.raspbian.org Git - gnome-shell.git/commitdiff
util: Add formatTime() helper method
authorFlorian Müllner <fmuellner@gnome.org>
Wed, 25 Feb 2015 15:43:59 +0000 (16:43 +0100)
committerFlorian Müllner <fmuellner@gnome.org>
Wed, 4 Mar 2015 12:55:01 +0000 (13:55 +0100)
Displaying a time is more complex than it appears at first glance:
it should respect the user's choice regarding 12- our 24-hour format (but
only when supported by the locale) and follow the LC_TIME rather than the
LC_MESSAGES setting.
So rather than getting it more or less right in various places, it makes
sense to defer to a helper method which hopefully does the right thing. The
method added by this patch is based on _formatTimestamp in telepathyClient
with some minor tweaks:
 - there's an additional params parameter which allows enforcing
   a time-only format, even on dates other than the current one
 - only a single desktop settings object is created and shared between
   invocations

https://bugzilla.gnome.org/show_bug.cgi?id=745111

js/misc/util.js

index e78c551fd3ea046db4cacfebe0a3fdae45bd5e3e..eabcbc620f67d80052eff33c1ae600891c160c69 100644 (file)
@@ -4,10 +4,12 @@ const Clutter = imports.gi.Clutter;
 const Gio = imports.gi.Gio;
 const GLib = imports.gi.GLib;
 const Lang = imports.lang;
+const Shell = imports.gi.Shell;
 const St = imports.gi.St;
 
 const Main = imports.ui.main;
 const Tweener = imports.ui.tweener;
+const Params = imports.misc.params;
 
 const SCROLL_TIME = 0.1;
 
@@ -38,6 +40,8 @@ const _urlRegexp = new RegExp(
         ')' +
     ')', 'gi');
 
+let _desktopSettings = null;
+
 // findUrls:
 // @str: string to find URLs in
 //
@@ -157,6 +161,82 @@ function _handleSpawnError(command, err) {
     Main.notifyError(title, err.message);
 }
 
+function formatTime(date, params) {
+    let now = new Date();
+
+    let daysAgo = (now.getTime() - date.getTime()) / (24 * 60 * 60 * 1000);
+
+    let format;
+
+    if (_desktopSettings == null)
+        _desktopSettings = new Gio.Settings({ schema_id: 'org.gnome.desktop.interface' });
+    let clockFormat = _desktopSettings.get_string('clock-format');
+    let hasAmPm = date.toLocaleFormat('%p') != '';
+
+    params = Params.parse(params, { timeOnly: false });
+
+    if (clockFormat == '24h' || !hasAmPm) {
+        // Show only the time if date is on today
+        if (daysAgo < 1 || params.timeOnly)
+            /* Translators: Time in 24h format */
+            format = N_("%H\u2236%M");
+        // Show the word "Yesterday" and time if date is on yesterday
+        else if (daysAgo <2)
+            /* Translators: this is the word "Yesterday" followed by a
+             time string in 24h format. i.e. "Yesterday, 14:30" */
+            // xgettext:no-c-format
+            format = N_("Yesterday, %H\u2236%M");
+        // Show a week day and time if date is in the last week
+        else if (daysAgo < 7)
+            /* Translators: this is the week day name followed by a time
+             string in 24h format. i.e. "Monday, 14:30" */
+            // xgettext:no-c-format
+            format = N_("%A, %H\u2236%M");
+        else if (date.getYear() == now.getYear())
+            /* Translators: this is the month name and day number
+             followed by a time string in 24h format.
+             i.e. "May 25, 14:30" */
+            // xgettext:no-c-format
+            format = N_("%B %d, %H\u2236%M");
+        else
+            /* Translators: this is the month name, day number, year
+             number followed by a time string in 24h format.
+             i.e. "May 25 2012, 14:30" */
+            // xgettext:no-c-format
+            format = N_("%B %d %Y, %H\u2236%M");
+    } else {
+        // Show only the time if date is on today
+        if (daysAgo < 1 || params.timeOnly)
+            /* Translators: Time in 12h format */
+            format = N_("%l\u2236%M %p");
+        // Show the word "Yesterday" and time if date is on yesterday
+        else if (daysAgo <2)
+            /* Translators: this is the word "Yesterday" followed by a
+             time string in 12h format. i.e. "Yesterday, 2:30 pm" */
+            // xgettext:no-c-format
+            format = N_("Yesterday, %l\u2236%M %p");
+        // Show a week day and time if date is in the last week
+        else if (daysAgo < 7)
+            /* Translators: this is the week day name followed by a time
+             string in 12h format. i.e. "Monday, 2:30 pm" */
+            // xgettext:no-c-format
+            format = N_("%A, %l\u2236%M %p");
+        else if (date.getYear() == now.getYear())
+            /* Translators: this is the month name and day number
+             followed by a time string in 12h format.
+             i.e. "May 25, 2:30 pm" */
+            // xgettext:no-c-format
+            format = N_("%B %d, %l\u2236%M %p");
+        else
+            /* Translators: this is the month name, day number, year
+             number followed by a time string in 12h format.
+             i.e. "May 25 2012, 2:30 pm"*/
+            // xgettext:no-c-format
+            format = N_("%B %d %Y, %l\u2236%M %p");
+    }
+    return date.toLocaleFormat(Shell.util_translate_time_string(format));
+}
+
 // lowerBound:
 // @array: an array or array-like object, already sorted
 //         according to @cmp